package service.impl;

import bean.*;
import mapper.IStudentMapper;
import org.apache.ibatis.session.SqlSession;
import service.IRoomService;
import service.IStudentService;

import java.util.List;

public class StudentServiceImpl extends BaseService implements IStudentService {


    @Override
    public CutPageBean<StudentBean> findByItem(int pageNo, String name, String className, String roomAddr) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        cutBean.setList(mapper.cutByItemList((pageNo - 1) * CutPageBean.PAGESIZE,
                CutPageBean.PAGESIZE, name, className, roomAddr));
        cutBean.setCount(mapper.cutByItemCount(name, className, roomAddr));
        session.close();
        return cutBean;
    }

    @Override
    public void add(StudentBean student, Integer classId, Integer roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student, classId, roomId);
        session.commit();
        session.close();
    }

    @Override
    public void delete(int studentId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delete(studentId);
        session.commit();
        session.close();
    }

    @Override
    public StudentBean findById(int studentId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean student = mapper.findById(studentId);
        session.close();
        return student;
    }

    @Override
    public void updateAddr(Integer studentId, Integer roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.updateAddr(studentId,roomId);
        session.commit();
        session.close();
    }


}
